import axios from 'axios'
import qs from 'qs'

// 创建一个包含默认配置的axios实例
const axiosInstance = axios.create({
  withCredentials: true, // 确保跨域请求时携带cookie
  headers: {
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
  }
})

// 配置qs序列化选项，确保中文正确编码
const qsOptions = {
  charset: 'utf-8',
  arrayFormat: 'brackets'
}

/** 获取用户列表 */
export const getUserList = (params: object) => {
    return axiosInstance.get('/api/user/listAll.do', { params })
}
/** 创建用户 */
export const createUser = (params: object) => {
    return axiosInstance.post('/api/user/create.do', qs.stringify(params, qsOptions), {
        headers: { 
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
    })
}
/** 更新用户 */
export const updateUser = (params: object) => {
    return axiosInstance.post('/api/user/update.do', qs.stringify(params, qsOptions), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    })
}
/** 删除用户 */
export const deleteUser = (userId: string) => {
    return axiosInstance.post('/api/user/delete.do', qs.stringify({ userId }, qsOptions), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    })
}
/** 根据用户ID获取用户信息 */
export const getUserById = (userId: string) => {
    return axiosInstance.get('/api/user/get.do', { params: { userId } })
}
/** 用户登录 */
export const login = (params: { email: string; password: string }) => {
  console.log('开始登录请求，参数:', params)
  return axiosInstance.post('/api/user/login.do', qs.stringify(params, qsOptions), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
  })
}
/** 用户注册 */
export const register = (params: {
  email: string;
  password: string;
  avatar?: string;
  gender?: number;
  age?: number;
  roleId: number;
  hotelName?: string;
}) => {
  console.log('开始注册请求，参数:', params)
  return axiosInstance.post('/api/user/register.do', qs.stringify(params, qsOptions), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
  })
}

/** 获取个人信息 */
export const getUserProfile = () => {
  return axiosInstance.get('/api/user/profile.do', {
    withCredentials: true
  })
}

/** 更新个人信息 */
export const updateUserProfile = (params: {
  newUserId?: string;
  avatar?: string;
  gender?: number;
  age?: number;
}) => {
  return axiosInstance.post('/api/user/updateProfile.do', qs.stringify(params, qsOptions), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    withCredentials: true
  })
}

/** 获取当前登录用户信息 */
export const getCurrentUser = () => {
    console.log('开始获取当前用户信息')
    return axiosInstance.get('/api/user/current.do', {
      withCredentials: true // 确保携带cookie
    })
}
/** 退出登录 */
export const logout = () => {
    return axiosInstance.get('/api/user/logout.do')
}
/** 获取用户角色 */
export const getUserRoles = (userId: string) => {
    return axiosInstance.get('/api/user/roles.do', { params: { userId } })
}
/** 获取角色列表 */
export const getRoleList = () => {
    // 直接返回固定的角色列表，符合后端的角色ID定义
    return Promise.resolve({
        data: {
            code: 200,
            message: '操作成功',
            data: {
                roles: [
                    { id: 1, name: '普通用户' },
                    { id: 2, name: '酒店商家' },
                    { id: 3, name: '管理员' }
                ]
            }
        }
    })
}
/** 删除角色（仅为兼容现有组件，实际不执行操作） */
export const deleteRole = (roleId: string) => {
    // 返回一个模拟的成功响应
    return Promise.resolve({
        data: {
            code: 200,
            message: '操作成功',
            data: true
        }
    })
}
/** 创建角色（仅为兼容现有组件，实际不执行操作） */
export const createRole = (params: object) => {
    // 返回一个模拟的成功响应
    return Promise.resolve({
        data: {
            code: 200,
            message: '操作成功',
            data: true
        }
    })
}

/** 更新角色（仅为兼容现有组件，实际不执行操作） */
export const updateRole = (params: object) => {
    // 返回一个模拟的成功响应
    return Promise.resolve({
        data: {
            code: 200,
            message: '操作成功',
            data: true
        }
    })
}
